<script>
  var relativeSortArray = function (arr1, arr2) {
    const { length } = arr2;
    const map = new Map();
    for (let i = 0; i < length; i++) {
      map.set(arr2[i], i + 1);
    }

    arr1.sort((a, b) => {
      const aIndex = map.get(a);
      const bIndex = map.get(b);
      if (aIndex && bIndex) {
        return aIndex - bIndex;
      } else if (!aIndex && !bIndex) {
        return a - b;
      } else {
        return aIndex ? -1 : 1;
      }
    })

    return arr1;
  };
  console.log(relativeSortArray([2,3,1,3,2,4,6,7,9,2,19],[2,1,4,3,9,6]));
</script>